package com.wise.service.cms.content.homedecorate;

import com.wise.entities.content.Decorate;
import com.wise.entities.content.DecorateModule;
import com.wise.entities.content.DecorateType;
import com.wise.framework.basic.DateTimeUtil;
import com.wise.framework.dao.SqlSelect;
import com.wise.framework.daomodel.DaoList;
import com.wise.framework.daomodel.DaoOne4Update;
import com.wise.framework.daomodel.DaoSave;
import com.wise.framework.session.CurrentUserId;
import com.wise.framework.session.GetAcceptLanguage;
import com.wise.summer.v1.cms.content.homedecorate.UpdateHomeDecorate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.sql.Timestamp;
import java.util.List;

import static com.wise.model.content.ContentErrMsgs.Err_000500000004;

@Service
@SummerService
public class UpdateHomeDecorateService implements SummerServiceBean<UpdateHomeDecorate> {

    @Override
    public void sum(UpdateHomeDecorate model) {
        String lang = GetAcceptLanguage.s(true);
        String userId = CurrentUserId.s(true);
        Timestamp now = DateTimeUtil.timestampNow();

        //执行查询，看是否为空，为空则新增，不为空则update
        SqlSelect sql = SqlSelect.selec("*");
        //type和module由后台负责处理，前台不传参
        sql.fromClassName(Decorate.class);
        sql.eq("type", DecorateType.PC.value());
        sql.eq("module", DecorateModule.HOME.value());
        sql.eqLang(Decorate._lang);
        //只执行查询数据，不分页
        List<Decorate> resourceList = DaoList.s(Decorate.class, sql);

        if (CollectionUtils.isEmpty(resourceList)) {
            //查询为空，那么就是新增
            Decorate decorate = new Decorate();
            decorate.setLang(lang);
            decorate.setType(DecorateType.PC.value());
            decorate.setModule(DecorateModule.HOME.value());
            decorate.setTitle(model.getTitle());
            decorate.setPageTitle(model.getPageTitle());
            //model = pc , type = index  ，这边实现的为pc首页逻辑
            //这边新增 草稿 还是 实际发布内容需要根据state判断
            //state为0则是保存草稿，前台不显示，state为1则是发布，前台显示
            //草稿字段为 content, 发布字段为publicContent
            if (model.getState() == 0) {
                decorate.setContent(model.getContent());
            } else if (model.getState() == 1) {
                decorate.setContent(model.getContent());
                decorate.setPublicContent(model.getContent());
            }

            //在新建时保存创建人和创建时间
            decorate.setCreatedBy(userId);
            decorate.setCreationDate(now);
            decorate.setLastUpdateBy(userId);
            decorate.setLastUpdateDate(now);

            decorate.setSeoTitle(model.getSeoTitle());
            decorate.setSeoDescribe(model.getSeoDescribe());

            decorate.setHeadTitle(model.getHeadTitle());

            DaoSave.s(decorate);

            model.setSummerResult(decorate.getId());
        } else {
            //如果查询不为空，那么执行更新的逻辑
            Decorate decorate = resourceList.get(0);
            decorate = DaoOne4Update.s(Decorate.class, decorate.getId(), Err_000500000004);
            decorate.setTitle(model.getTitle());
            if (model.getState() == 0){
                decorate.setContent(model.getContent());
            } else if (model.getState() == 1){
                decorate.setContent(model.getContent());
                decorate.setPublicContent(model.getContent());
            }

            //在更新的时候保存更新人和更新时间
            decorate.setLastUpdateBy(userId);
            decorate.setLastUpdateDate(now);
            decorate.setLang(lang);
            decorate.setSeoTitle(model.getSeoTitle());
            decorate.setSeoDescribe(model.getSeoDescribe());
            decorate.setHeadTitle(model.getHeadTitle());
            decorate.setPageTitle(model.getPageTitle());

            DaoSave.s(decorate);

            model.setSummerResult(decorate.getId());
        }
    }
}
